/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * z_DIE.java
 *
 * Created on Oct 24, 2010, 2:31:35 PM
 */

package Java;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author shyan
 */
public class CASH_SUMMARY extends javax.swing.JFrame {

    private String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private String userID = "restaurant";
    private String userPW = "restaurant";
    private Connection con;
    private Statement stmt;
    private ResultSet rset;
    Double total = 0.00;
    Double price;

    /** Creates new form z_DIE */
    public CASH_SUMMARY() {
        initComponents();
        setLocationRelativeTo(null);
        Entity();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        comboBOX = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        payButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        backButton = new javax.swing.JButton();
        GoButton = new javax.swing.JButton();
        totalField = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(700, 500));
        getContentPane().setLayout(null);

        jLayeredPane1.setMinimumSize(new java.awt.Dimension(700, 500));

        jTable1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(144, 137, 153), 1, true));
        jTable1.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 13));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Dish code", "Dish name", "Unit ordered", "Price"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);

        jScrollPane1.setBounds(60, 170, 600, 190);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(10, 70, 690, 20);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 24));
        jLabel1.setText("Cashier  Profile");
        jLabel1.setBounds(50, 20, 270, 40);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        comboBOX.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboBOX.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                comboBOXPropertyChange(evt);
            }
        });
        comboBOX.setBounds(220, 110, 110, 30);
        jLayeredPane1.add(comboBOX, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        jLabel2.setText("Current served table:");
        jLabel2.setBounds(80, 110, 140, 30);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Search", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Serif Condensed", 3, 16))); // NOI18N
        jLabel3.setBounds(60, 80, 290, 80);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        payButton.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        payButton.setText("PAY");
        payButton.setBounds(470, 420, 80, 40);
        jLayeredPane1.add(payButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator2.setBounds(4, 406, 690, 10);
        jLayeredPane1.add(jSeparator2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        backButton.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        backButton.setText("BACK");
        backButton.setBounds(570, 420, 80, 40);
        jLayeredPane1.add(backButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        GoButton.setText("GO!");
        GoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GoButtonActionPerformed(evt);
            }
        });
        GoButton.setBounds(370, 110, 60, 30);
        jLayeredPane1.add(GoButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        totalField.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 15));
        totalField.setBounds(510, 360, 150, 30);
        jLayeredPane1.add(totalField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        jLabel4.setText("Total amount : RM");
        jLabel4.setBounds(390, 360, 120, 30);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/abstract2.jpg"))); // NOI18N
        jLabel5.setBounds(0, 0, 700, 500);
        jLayeredPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(jLayeredPane1);
        jLayeredPane1.setBounds(0, 0, 700, 500);

        pack();
    }// </editor-fold>

    private void comboBOXPropertyChange(java.beans.PropertyChangeEvent evt) {                                        

        
    }                                       

    private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         

        String a = (String) comboBOX.getSelectedItem();
        String GetPLQuery="SELECT * FROM ORDER_LIST WHERE MENU_CODE='"+a+"'";
        ResultSet get3=getResultSet(GetPLQuery);
        try {
            while (get3.next()) {
                jTable1.setValueAt(get3.getString("MENU_CODE").trim(), get3.getRow() - 1, 0);
                jTable1.setValueAt(get3.getString("FOOD_NAME").trim(), get3.getRow() - 1, 1);
                jTable1.setValueAt(get3.getString("FOOD_QUANTITY").trim(), get3.getRow() - 1, 2);
                jTable1.setValueAt(get3.getString("FOOD_PRICE").trim(), get3.getRow() - 1, 3);
                price = Double.parseDouble(get3.getString("FOOD_PRICE"));
                total += price;
                //System.out.println(get3.getRow());
            }
            totalField.setText(total.toString());

        } catch (SQLException ex) {
            Logger.getLogger(CASH_SUMMARY.class.getName()).log(Level.SEVERE, null, ex);
        }
        // TODO add your handling code here:
    }                                        

    void resetTable()
   {
       for (int row = 0; row < 30; row++)
       {
           for (int col = 0; col < 3; col++)
           {
                jTable1.setValueAt(null,row,col);
           }
       }
    }

    private void Entity()
    {
        comboBOX.removeAllItems();
        String SQL = "SELECT DISTINCT MENU_CODE FROM ORDER_LIST";
        String temp = "";
        try
        {
         rset=getResultSet(SQL);
         while (rset.next())
         {
             //String a = rset.getString(1);
             //if(!rset.getString(1).equals(temp))
             //{
                if(rset.getString(1).isEmpty()==false)
                {
                    comboBOX.addItem(rset.getString(1));
                }
             //}
         }
          stmt.close();
          con.close();
        }
        catch(Exception ex)
        {
           System.out.println("=.=");
        }
     }

    private ResultSet getResultSet(String doubt)
    {
        connectDatabase();
        ResultSet outcome = null;
        try
        {
            stmt = con.createStatement();
            outcome = stmt.executeQuery(doubt);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(CASH_SUMMARY.class.getName()).log(Level.SEVERE, null, ex);
        }
        return outcome;
    }

    private void connectDatabase()
    {
        try
        {
            Class.forName("oracle.jdbc.OracleDriver");
            con=DriverManager.getConnection(url, userID, userPW);
        }


        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(CASH_SUMMARY.class.getName()).log(Level.SEVERE, null, ex);
        }        catch (SQLException ex)
        {
            Logger.getLogger(CASH_SUMMARY.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CASH_SUMMARY().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton GoButton;
    private javax.swing.JButton backButton;
    private javax.swing.JComboBox comboBOX;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton payButton;
    private javax.swing.JLabel totalField;
    // End of variables declaration

}
